<template>
  <div class="TreeSelect">
    <div class="coent" style="font-size: 18px"></div>
  </div>
</template>
<script>
import {
  ExecProcedure,
  CreateProcess,
  PlatSendTask,
  PrepareCreateProcess,
  FileServerProxy,
} from "../../utils/api";
export default {
  data() {
    return {
      items: [],
      ifID: "",
      TASK_ID: "",
      activeIds: [],
      activeIndex: 0,
      tableData: [],
      indexData: [],
      tableData1: [],
      code1: "",
      xcfy: "",
      rowGuid: "",
      iSKNH: "",
    };
  },
  mounted() {
    if (ZWJSBridge) {
      ZWJSBridge.setTitle({
        title: "选择人员",
      })
        .then((res) => {})
        .catch((err) => {});
    }
  },
  methods: {
    getData: async function (data) {
      debugger
      //自动批转
      //判断如果是第一个环节，需要重新写方法，先调用创建流程方法，再自动发送，如果存储过程，则要调用存储过程
     if(this.$route.query.list=='1'){
      let params = {
          accessCode: "0a709d649c314903a72225edb8bc8c2a",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000",
          params: {
            taskId: data.TASK_ID,
            direction: 0, //0是发送  1是打回
            token: window.localStorage.getItem("token"),
          },
        };
        const res = await FileServerProxy(params);
        var  datasendForm = JSON.parse(res.data.data);
        console.log(datasendForm)
      if(this.$route.query.data.CBPD=="1"){//这个值也是从提交组件传过来的
        //发送结束
         if (datasendForm.SendData && datasendForm.SendData.length > 0){
          datasendForm.SendData.forEach((nodeItem) => {
                //镇街备案
                var maxtnid; //获取tnid最大值
                if (
                  nodeItem.WFNODEGUID ==
                    "{04FF1A3D-823C-4653-95D6-853C37450D44}" &&
                  nodeItem.PTNID == -1
                ) {
                  var tnid = nodeItem.TNID;
                  var newItem = {};
                  maxtnid = maxtnid + 1;
                  newItem.TNID = maxtnid;
                  newItem.PTNID = tnid;
                  newItem.TEXT = nodeItem.TEXT;
                  newItem.USERID = nodeItem.USERID;
                  newItem.AUTOCHECKED = true;
                  newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                  newItem.WFNODENAME = nodeItem.WFNODENAME;
                  newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                  newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                  newItem.ISUSER = 1;
                  this.indexData.push(newItem);
                }
              });
         }

      }else{
       //发送危房整治
         let str1 = data.SSXZ;//当前行数据
          let paer = {
            procName: "up_management",
            dtParams: JSON.stringify([{ I_USERID: "'"+str1+"'", TYPE: 16 }]),
            token: window.localStorage.getItem("token"),
          };
          let params4 = {
            accessCode: "69db6bece3a7480592e2ec753d1e335e",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: paer,
          };
          const resEX = await FileServerProxy(params4);
          let proEX = JSON.parse(resEX.data.data);
          if (proEX.Data && proEX.Data.length > 0) {
              datasendForm.SendData.forEach((nodeItem) => {
                //镇街备案
                var maxtnid; //获取tnid最大值
                if (
                  nodeItem.WFNODEGUID ==
                    "{665621C1-8931-4819-9770-FA6DD93619BA}" &&
                  nodeItem.PTNID == -1
                ) {
                  var tnid = nodeItem.TNID;
                  var newItem = {};
                  maxtnid = maxtnid + 1;
                  newItem.TNID = maxtnid;
                  newItem.PTNID = tnid;
                  newItem.TEXT = proEX.Data[0].ST_NAME;
                  newItem.USERID = proEX.Data[0].ST_ID;
                  newItem.AUTOCHECKED = true;
                  newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                  newItem.WFNODENAME = nodeItem.WFNODENAME;
                  newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                  newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                  newItem.ISUSER = 1;
                  this.indexData.push(newItem);
                }
              });
          }

      }

       this.handleGoToNext();
     }
     else if (this.iSKNH) {
        //判断是否是困难户
        let paramsknh = {
          accessCode: "2c824ce489ff4cffaa0d77082e6fb648",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000",
          params: {
            wfGuid: "{B1D50CB5-FA5D-4A07-93F6-D66543EB2916}",
            entityRowGuid: data.ROW_GUID,
            token: window.localStorage.getItem("token"),
          },
        };
        const resknh = await FileServerProxy(paramsknh);
        var SendFormDataknh = JSON.parse(resknh.data.data);
        if (SendFormDataknh && SendFormDataknh.SendData.length > 0) {
          //提交镇街审核 掉存储过程
          let str1 = data.ZCITY;
          let paerknh = {
            procName: "up_processusername",
            dtParams: JSON.stringify([{ I_USERID: str1, TYPE: 6 }]),
            token: window.localStorage.getItem("token"),
          };
          let params4 = {
            accessCode: "69db6bece3a7480592e2ec753d1e335e",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: paerknh,
          };
          const resEXknh = await FileServerProxy(params4);
          let proEXknh = JSON.parse(resEXknh.data.data);
          if (proEXknh && proEXknh.Data.length > 0) {
            SendFormDataknh.SendData.forEach((nodeItem) => {
              var maxtnid = 0; //获取tnid最大值
              if (
                nodeItem.WFNODEGUID ==
                  "{87C36CE3-495B-4C7D-88FA-2CF2E7F5672A}" &&
                nodeItem.PTNID == -1
              ) {
                proEXknh.Data.forEach((item) => {
                  var tnid = nodeItem.TNID;
                  var newItem = {};
                  maxtnid = maxtnid + 1;
                  newItem.TNID = maxtnid;
                  newItem.PTNID = tnid;
                  newItem.TEXT =item.USERNAME;
                  newItem.USERID =item.USERID;
                  newItem.AUTOCHECKED = true;
                  newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                  newItem.WFNODENAME = nodeItem.WFNODENAME;
                  newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                  newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                  newItem.ISUSER = 1;
                  this.indexData.push(newItem);
                });
              }
            });
          }
          this.handleGoToNext();
        }
      } else if (this.$route.query.list != "true") {
        //获取到人员列表
        let params = {
          accessCode: "2c824ce489ff4cffaa0d77082e6fb648",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000",
          params: {
            wfGuid: "{DF6C42D5-2BED-473B-9C4D-DA8C42DAF7E9}",
            entityRowGuid: data.ROW_GUID,
            token: window.localStorage.getItem("token"),
          },
        };
        const res = await FileServerProxy(params);
        let SendFormData = JSON.parse(res.data.data);
        if (SendFormData.SendData.length > 0) {
          //判断是否是镇街审核

          //是鉴定公司
          let str1 = data.TYSHXYDM;
          let paer = {
            procName: "up_processusername",
            dtParams: JSON.stringify([{ I_JDJGID: "'"+str1+"'", TYPE: 3 }]),
            token: window.localStorage.getItem("token"),
          };
          let params4 = {
            accessCode: "69db6bece3a7480592e2ec753d1e335e",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: paer,
          };
          const resEX = await FileServerProxy(params4);
          let proEX = JSON.parse(resEX.data.data);
          if (proEX.Data && proEX.Data.length > 0) {
            SendFormData.SendData.forEach((nodeItem) => {
              //鉴定公司
              var maxtnid; //获取tnid最大值
              if (
                nodeItem.WFNODEGUID ==
                  "{C856358A-5B9A-42F0-814D-5BF3F63F5929}" &&
                nodeItem.PTNID == -1
              ) {
                var tnid = nodeItem.TNID;
                var newItem = {};
                maxtnid = maxtnid + 1;
                newItem.TNID = maxtnid;
                newItem.PTNID = tnid;
                newItem.TEXT = proEX.Data[0].USERNAME;
                newItem.USERID = proEX.Data[0].USERID;
                newItem.AUTOCHECKED = true;
                newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                newItem.WFNODENAME = nodeItem.WFNODENAME;
                newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                newItem.ISUSER = 1;
                this.indexData.push(newItem);
              }
            });
          }
        }

        //调用发送接口
        this.handleGoToNext();
      } else {
        //自动批转通过存储过程来获取人员并实现自动发送

        //获取发送人员的接口
        let params = {
          accessCode: "0a709d649c314903a72225edb8bc8c2a",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000",
          params: {
            taskId: data.TASK_ID,
            direction: 0, //0是发送  1是打回
            token: window.localStorage.getItem("token"),
          },
        };
        const res = await FileServerProxy(params);
        let datasendForm = JSON.parse(res.data.data);
        //判断等级 发送不同的环节
        if (datasendForm.SendData && datasendForm.SendData.length > 0) {
          if (
            this.$route.query.data.JDDJ == "243" ||
            this.$route.query.data.JDDJ == "244"
          ) {
            //调用存储过程
            let str1 = this.$route.query.data.ZCITY;
            let paer = {
              procName: "up_processusername",
              dtParams: JSON.stringify([{ I_USERID: str1, TYPE: 2 }]),
              token: window.localStorage.getItem("token"),
            };
            let procNames = {
              accessCode: "69db6bece3a7480592e2ec753d1e335e",

              cityCode: window.localStorage.getItem("citycode")
                ? window.localStorage.getItem("citycode")
                : "441300000000",
              params: paer,
            };
            const res = await FileServerProxy(procNames);
            let proZJSH = JSON.parse(res.data.data);
            if (proZJSH.Data && proZJSH.Data.length > 0) {
              datasendForm.SendData.forEach((nodeItem) => {
                //镇街备案
                var maxtnid; //获取tnid最大值
                if (
                  nodeItem.WFNODEGUID ==
                    "{5EB5555C-D4AB-411A-B781-0DD95F96E544}" &&
                  nodeItem.PTNID == -1
                ) {
                  var tnid = nodeItem.TNID;
                  var newItem = {};
                  maxtnid = maxtnid + 1;
                  newItem.TNID = maxtnid;
                  newItem.PTNID = tnid;
                  newItem.TEXT = proZJSH.Data[0].USERNAME;
                  newItem.USERID = proZJSH.Data[0].USERID;
                  newItem.AUTOCHECKED = true;
                  newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                  newItem.WFNODENAME = nodeItem.WFNODENAME;
                  newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                  newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                  newItem.ISUSER = 1;
                  this.indexData.push(newItem);
                }
              });
            }
          } else if (
            this.$route.query.data.JDDJ == "245" ||
            this.$route.query.data.JDDJ == "246"
          ) {
            let str1 = this.$route.query.data.ZCITY;
            let paer = {
              procName: "up_processusername",
              dtParams: JSON.stringify([{ I_USERID: str1, TYPE: 2 }]),
              token: window.localStorage.getItem("token"),
            };
            let procNameBA = {
              accessCode: "69db6bece3a7480592e2ec753d1e335e",

              cityCode: window.localStorage.getItem("citycode")
                ? window.localStorage.getItem("citycode")
                : "441300000000",
              params: paer,
            };
            const res = await FileServerProxy(procNameBA);
            let proZJBA = JSON.parse(res.data.data);
            if (proZJBA.Data && proZJBA.Data.length > 0) {
              datasendForm.SendData.forEach((nodeItem) => {
                //镇街审核
                var maxtnid; //获取tnid最大值
                if (
                  nodeItem.WFNODEGUID ==
                    "{E493C582-D97E-4C0D-B6B8-8FF974E8306B}" &&
                  nodeItem.PTNID == -1
                ) {
                  var tnid = nodeItem.TNID;
                  var newItem = {};
                  maxtnid = maxtnid + 1;
                  newItem.TNID = maxtnid;
                  newItem.PTNID = tnid;
                  newItem.TEXT = proZJBA.Data[0].USERNAME;
                  newItem.USERID = proZJBA.Data[0].USERID;
                  newItem.AUTOCHECKED = true;
                  newItem.WFNODEGUID = nodeItem.WFNODEGUID;
                  newItem.WFNODENAME = nodeItem.WFNODENAME;
                  newItem.WFNODETYPE = nodeItem.WFNODETYPE;
                  newItem.SINGLEASSIGN = nodeItem.SINGLEASSIGN;
                  newItem.ISUSER = 1;
                  this.indexData.push(newItem);
                }
              });
            }
          }
        }
        this.handleGoToNext();
      }
    },
    onClickLeft() {
      this.$router.back(-1);
    },
    handleGoToNext: async function () {
      //判断当前是否选择了人员
      if (this.indexData.length > 0) {
        if(this.iSKNH&&this.$route.query.list!="1"){
            let params = {
            accessCode: "3ec88931e2bf429d9ceadff8697b6772",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: {
              wfGuid: "{B1D50CB5-FA5D-4A07-93F6-D66543EB2916}",
              entityRowGuid: this.$route.query.data.ROW_GUID,
              token: window.localStorage.getItem("token"),
            },
          };
          const resCreate = await FileServerProxy(params);
          let resCreateData = JSON.parse(resCreate.data.data);
          this.$route.query.data.TASK_ID = resCreateData.TaskId;
        }
       else if (this.$route.query.list != "true"&&this.$route.query.list!="1") {
          let params = {
            accessCode: "3ec88931e2bf429d9ceadff8697b6772",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: {
              bizGuid: "845b7b9f-7630-4511-a1aa-0f8e8355cf89",
              wfGuid: "{DF6C42D5-2BED-473B-9C4D-DA8C42DAF7E9}",
              entityRowGuid: this.$route.query.data.ROW_GUID,
              token: window.localStorage.getItem("token"),
            },
          };
          const resCreate = await FileServerProxy(params);
          let resCreateData = JSON.parse(resCreate.data.data);
          this.$route.query.data.TASK_ID = resCreateData.TaskId;
        }
        if (this.$route.query.data.TASK_ID) {
          let plantask = {
            accessCode: "c52bcf7cf3fb42bb9fbcd9b91e97d411",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: {
              taskId: this.$route.query.data.TASK_ID,
              direction: 0,
              wfBoxGuid: "",
              actionText: "同意",
              taskName: "",
              wfGuid: this.xcfy,
              sendNodes: JSON.stringify(this.indexData),
              token: window.localStorage.getItem("token"),
            },
          };
          const respl = await FileServerProxy(plantask);
          let data = JSON.parse(respl.data.data).Success;
          //发送后逻辑
          if(this.$route.query.list=='1'){
            let paerknh = {
              procName: "up_management",
              dtParams: JSON.stringify([{ I_USERID: "'" + this.$route.query.data.ROW_GUID + "'", TYPE: 9 }]),
              token: window.localStorage.getItem("token"),
            };
            let params4 = {
              accessCode: "69db6bece3a7480592e2ec753d1e335e",
              cityCode: window.localStorage.getItem("citycode")
                ? window.localStorage.getItem("citycode")
                : "441300000000",
              params: paerknh,
            };
            const resEXknh = await FileServerProxy(params4);
          }
          if (data) {
            this.$toast.success("成功！");
            if (this.$route.query.list=='1'){
              setTimeout(() => {
                this.$router.push({
                  path: '/FeatureList',
                });
              }, 500);
            }else{
              setTimeout(() => {
                this.$router.push({
                  path: '/ZLJIndex',
                });
              }, 500);
            }


          }
        }
      } else {
        this.$toast.fail("请选择人员！");
      }
    },
  },
  mounted() {
    //接收浙里办传来的参数
    debugger;
    if (this.$route.query.data && this.$route.query.data.KNLX) {
      if (this.$route.query.data.KNLX == 5) {
        this.iSKNH = true;
      } else {
        this.iSKNH = false;
      }
    }
    this.getData(this.$route.query.data);

    // //调用视图减少人员选择
  },
};
</script>
<style lang="scss" scoped>
.TreeSelect {
  height: 100vh;
  background: #fff;
  font-size: 0.2em;
}
</style>
